<template>
  <div>
    <!--    搜索院系信息-->
    <div id="search" style="text-align: right">
      <el-form :inline="true" id="demo-form-inline">
        <el-form-item label="院系名称">
          <el-input @keyup.enter.native="handleSearch" v-model="formDate.depname" placeholder="请输入院系名称"></el-input>
        </el-form-item>
        <el-form-item label="院系成立日期" style="margin-left: 100px;">
          <div class="block">
            <el-date-picker
              v-model="formDate.date"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item style="margin-left: 100px;margin-right: 150px;">
          <el-button style="width: 150px;" type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <!--    展示院系信息-->
    <div id="list">
      <el-table :data="depList" style="width: 100%" :stripe="true" :border="true"
                :highlight-current-row="true" height="420">
        <el-table-column label="院系ID" width="150px" prop="id"></el-table-column>
        <el-table-column label="院系名称" prop="depname"></el-table-column>
        <el-table-column label="成立日期" prop="date"></el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="scope">
            <el-button size="mini" @click="change(scope.row.id)">修改</el-button>
            <el-button size="mini" type="danger" @click="open(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    修改院系信息-->
    <el-dialog title="修改院系信息" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="院系ID" :label-width="formLabelWidth">
          <el-input placeholder="请输入内容" v-model="department.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="院系名称" :label-width="formLabelWidth">
          <el-input v-model="department.depname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="成立时间" :label-width="formLabelWidth">
          <el-col :span="11">
            <el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                            v-model="department.date"></el-date-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--    分页-->
    <div id="block">
      <el-pagination background
                     :current-page.sync="pagination.page"
                     :page-size="pagination.pageSize"
                     :total="pagination.total"
                     :page-sizes="[7,15,30]"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     layout="total, prev, pager, next, sizes">
      </el-pagination>
    </div>
  </div>
</template>

<script>

import departmentApi from "@/api/department";
import qs from "qs";
export default {
  name: "list",
  data() {
    return {
      depList: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      formDate: {
        depname: "",
        date: ""
      },
      pagination: {
        total: 0,
        page: 1,
        pageSize: 7
      },
      timeout: null,
      department: {
        id: "",
        depname: "",
        date: ""
      },
    }
  },
  computed: {
    startDate() {
      return this.formDate.date == null ? '' : this.formDate.date[0]
    },
    endDate() {
      return this.formDate.date == null ? '' : this.formDate.date[1]
    }
  },
  mounted() {
    this.queryDepartment()
  },
  methods: {
    //页码改变时的回调函数
    handleCurrentChange(page) {
      this.pagination.page = page;
      this.queryDepartment();
    },
    //页面大小改变时的回调函数
    handleSizeChange(pageSize) {
      this.pagination.pageSize = pageSize;
      this.queryDepartment();
    },
    //打开确认删除的提示框
    open(id) {
      this.$confirm('此操作将永久删除该院系, 是否继续?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(id);
      })
    },
    //点击修改按钮
    change(id) {
      this.selectDepartmentById(id)
      this.dialogFormVisible = true;
    },
    //修改院系信息函数
    handleEdit() {
      departmentApi.updateDepartment(qs.stringify(this.department)).then(resp => {
        if (resp.code === 6666) {
          this.$message.success("修改成功！")
          this.queryDepartment()
          this.dialogFormVisible = false;
        } else {
          this.$message.info(resp.msg)
        }
      }).catch(() => {
        this.$message.error("修改失败！")
      });
    },
    //删除院系信息函数
    handleDelete(id) {
      departmentApi.deleteDepartment(id).then(resp => {
        if (resp.code === 6666) {
          this.$message.success('删除成功!');
          this.queryDepartment();
        } else {
          this.$message.info('删除失败')
        }
      }).catch(error => {
        this.$message.error('删除出错');
      })
    },
    //点击查询时回到第一页
    handleSearch() {
      this.pagination.page = 1;
      this.queryDepartment()
    },
    //分页查询院系链表
    queryDepartment() {
      const pagination = {
        depname: this.formDate.depname,//搜索框中的值
        startCreateDate: this.startDate,//要搜索的时间段的开始日期
        endCreateDate: this.endDate,//要搜索的时间段的结束日期
        page: this.pagination.page,//当前页码
        pageSize: this.pagination.pageSize,//每页多少条数据
      }
      departmentApi.getPageDepartmentList(pagination).then(resp => {
        if (resp.code === 6666) {
          this.depList = resp.data;
          this.pagination.page = resp.pageInfo.page;
          this.pagination.pageSize = resp.pageInfo.pageSize;
          this.pagination.total = resp.pageInfo.total;
        } else {
          this.$message.info('获取院系列表失败')
        }
      }).catch(() => {
        this.$message.error("查询院系列表出错！")
      })
    },
    //取消修改的提示信息
    handleCancle() {
      this.dialogFormVisible = false;
    },
    //根据id查询院系信息
    selectDepartmentById(id){
      departmentApi.getById(id).then(resp=>{
        if (resp.code===6666){
          this.department=resp.data
        }else {
          this.$message.warning('查询院系信息失败')
        }
      }).catch(()=>{
        this.$message.error('查询院系信息出错')
      })
    }
  },
}
</script>
<style scoped>

#list {
  margin-left: 20px;
  margin-right: 20px;
}

#search {
  margin-left: 100px;
  margin-top: 60px;
}

#block {
  margin-top: 20px;
  margin-left: 20px;
}

#demo-form-inline {
  margin-bottom: 0;
}

.el-divider {
  margin: 10px 0;
}
</style>
